package com.thti.mecool.client.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则匹配
 * 
 * @author weiguo.ren
 * 
 */
public class RegexPattern {

	public static final String URL = "[a-zA-z]+://[^\\s]*";
	public static final String EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	public static final String LOGINNAME = "^[a-zA-Z0-9_]{3,20}$";
	public static final String PASSWORD = "^[a-zA-Z0-9_]{3,20}$";
//	public static final String PASSWORD = "^[a-zA-Z0-9_]$";
	public static final String CHARS = "[`~!@#$%^&*()+ =|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
	public static final String TEL = "\\d{3}-\\d{8}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|\\d{3}-\\d{7}|\\d{3}\\d{8}|\\d{4}\\d{8}|\\d{3}\\d{7}|\\d{4}\\d{7}";
	public static final String ZIP_CODE = "[1-9]\\d{5}(?!\\d)";
	public static final String ID_CARD_NO = "\\d{15}|\\d{18}|\\d{17}[Xx]";
//	public static final String ID_CARD_NO = "^[a-zA-Z0-9_]{14,20}$";
	public static final String PHONE = "^((13[0-9])|(14[0-9])|(15[0-9])|(18[0,0-9]))\\d{8}$";
	public static final String NUMBER="^[0-9\\.]+$";
	public static final String ID_CARD="^[0-9Xx]+$";
	public static boolean isNumber(String value){
		int j=0;
		for(int i=0;i<value.length();i++){
			if(value.charAt(i)==46){
				j++;
				if(j==2){
					return false;}
			}
			if(value.charAt(0)==46||value.charAt(value.length()-1)==46)
				return false;
		}
		return value.matches(NUMBER);
	}
	
	
	public static boolean isIDCard(String value){
		return value.matches(ID_CARD);
	}
	
	/**
	 * 匹配手机号码
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isPhone(String value) {
		return value.matches(PHONE);
	}

	/**
	 * 匹配身份证
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isIdCardNo(String value) {
		return value.matches(ID_CARD_NO);
	}

	/**
	 * 匹配密码是否合法(密码长度4-10字母和数字)
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isPassWord(String value) {
		return value.matches(PASSWORD);
	}

	/**
	 * 匹配帐号是否合法(字母开头，允许4-10字节，允许字母数字下划线)
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isLoginName(String value) {
		return value.matches(LOGINNAME);
	}

	/**
	 * 匹配中国邮政编码
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isZipCode(String value) {
		return value.matches(ZIP_CODE);
	}

	/**
	 * 匹配国内电话号码
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isTel(String value) {
		if (value.trim().equals("")) {
			return true;
		}
		return value.matches(TEL);
	}

	/**
	 * 检测非法字符
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isChars(String str) {
		Pattern p = Pattern.compile(CHARS);
		Matcher m = p.matcher(str);
		return m.find();
	}

	/**
	 * 检测Email
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmail(String email) {
		return Pattern.matches(EMAIL, email);
	}

	public static boolean isUrl(String url) {
		return Pattern.matches(URL, url);
	}

	public static String encode(String regex) {
		if (regex.indexOf("[") != -1)
			regex = regex.replace("[", "\\[");
		if (regex.indexOf("]") != -1)
			regex = regex.replace("]", "\\]");
		if (regex.indexOf("(") != -1)
			regex = regex.replace("(", "\\(");
		if (regex.indexOf(")") != -1)
			regex = regex.replace(")", "\\)");
		return regex;
	}

}
